package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.constants.ServeConstants;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    private final ServeMapper serveMapper;
    private final IServeItemService serveItemService;
    private final RegionMapper regionMapper;
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO queryReqDTO) {
        Page<ServeResDTO> page = new Page<>(queryReqDTO.getPageNo(), queryReqDTO.getPageSize());
        List<ServeResDTO> list = serveMapper.list(page, queryReqDTO.getRegionId());
        return PageResult.of(list,queryReqDTO.getPageSize().intValue(),page.getPages(),page.getTotal());
    }

    @Override
    @Transactional
    public void ListBatch(List<ServeUpsertReqDTO> serveList) {
        List<Serve> list = new ArrayList<>();
        //遍历集合查看是否有未启用状态
        for (ServeUpsertReqDTO serveDto : serveList){
            ServeItem serveItem = serveItemService.getById(serveDto.getServeItemId());
            if(ObjectUtil.isEmpty(serveItem)){
                throw new RuntimeException("该服务未启用无法添加到区域下使用");
            }

            //检验是否重复添加
            Integer count = lambdaQuery()
                    .eq(Serve::getRegionId, serveDto.getRegionId())
                    .eq(Serve::getServeItemId, serveDto.getServeItemId())
                    .count();
            if(count > 0){
                throw new RuntimeException(serveItem.getName()+"服务已存在");
            }
            //查询区域code
            Serve serve = BeanUtil.toBean(serveDto, Serve.class);
            Region region = regionMapper.selectById(serveDto.getRegionId());
            serve.setCityCode(region.getCityCode());
            list.add(serve);
        }
        super.saveBatch(list);
    }

    @Override
    public void update(Long id, BigDecimal price) {
        super.update(Wrappers.<Serve>lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price));
    }

    @Override
    public void activate(Long id) {
        super.update(Wrappers.<Serve>lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, ServeConstants.SERVE_ON_SALE));
    }
    @Override
    public void disable(Long id) {
        super.update(Wrappers.<Serve>lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, ServeConstants.SERVE_OFF_SALE));
    }
    @Override
    public void onHot(Long id) {
        super.update(Wrappers.<Serve>lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, ServeConstants.SERVE_ON_HOT));
    }
    @Override
    public void offHot(Long id) {
        super.update(Wrappers.<Serve>lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, ServeConstants.SERVE_OFF_HOT));
    }
}
